package com.zzptc.mzb.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzptc.mzb.myapplication.R;
import com.zzptc.mzb.myapplication.databinding.RecipeBinding;
import com.zzptc.mzb.myapplication.entity.ResultBean;

import java.util.List;

public class MyRecyclerviewAadpter extends RecyclerView.Adapter<MyRecyclerviewAadpter.ViewHolder> {
        private List<ResultBean>resultBeanList;
        private Context context;

    public MyRecyclerviewAadpter(List<ResultBean> resultBeanList, Context context) {
        this.resultBeanList = resultBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //View.inflate(context, R.layout.recipe,null);
       RecipeBinding binding= DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.recipe,
                parent,
                false);
        return new ViewHolder(binding);
    }

    @Override//绑定数据
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //将数据拿出来
        ResultBean resultBean=resultBeanList.get(position);
        holder.recipeBinding.ivTitle.setText(resultBean.getTitle());
        Glide.with(context)
                .load(resultBean.getCover())
                .into(holder.recipeBinding.ivPic);

    }

    @Override
    public int getItemCount() {
        return resultBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RecipeBinding recipeBinding;
        public ViewHolder(@NonNull RecipeBinding recipeBinding) {
            super(recipeBinding.getRoot());
            this.recipeBinding=recipeBinding;
        }
    }
}
